#!/bin/mksh

# don't run if not interactive
[[ $- != *i* ]] && return

# aliases

. ${HOME}/.aliases
alias echo='builtin print'
alias type='builtin whence -v'

export OPEN=${HOME}/bin/open

bind '^L=clear-screen'
bind '^Z=redraw'

# customise your favourite editor here; the first one found is used
for EDITOR in "${EDITOR:-}" vis ed vi; do
    EDITOR=$(\\builtin whence -p "$EDITOR") || EDITOR=
    [[ -n $EDITOR && -x $EDITOR ]] && break
    EDITOR=
done

# aliasing dat ls baby
\\builtin alias ls=ls l='ls -F --color=auto' la='l -a' ll='l -l' lo='l -alo'
\: "${HOSTNAME:=$(\\builtin ulimit -c 0; \\builtin print -r -- $(hostname \
    2>/dev/null))}${EDITOR:=/bin/ed}${TERM:=vt100}${USER:=$(\\builtin ulimit \
    -c 0; id -un 2>/dev/null)}${USER:=?}"
[[ $HOSTNAME = ?(?(ip6-)localhost?(6)) ]] && HOSTNAME=nil; \\builtin unalias ls
\\builtin export EDITOR HOSTNAME TERM USER

# histfile
export HISTFILE=${HOME}/.hist \
    {SAVEHIST,HISTSIZE}=1000 \
    HISTCONTROL=ignoreboth

ulimit -c 0

# PS1
HOST=`hostname`
#export PS1='[${USER}@${HOST} ${PWD###*/}]$ '

cd() {
    if [ $# -eq 0 ] ; then
        builtin cd ${HOME}
    else
        # lazy cd-ing functions
        builtin cd "$@" ||
        builtin cd "$@"* ||
        builtin cd *"$@" ||
        builtin cd *"$@"*
        #for i in 0 1 2 3 4 5 6 7 8 ; do
        #    builtin cd $(find . -maxdepth $(($i + 1)) -mindepth $i -type d -iname "*$@" | head -n 1) && break
        #done
    fi 2> /dev/null # errors INTO THE VOID
    export PS1="$(_get_PS1)"
}

_parse_branch() {
    local branch=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
    [ -n "$branch" ] && echo -n " ($branch)"
} 2> /dev/null

_x="$(echo -n \\001)"
_get_PS1() {
    case ${PWD} in
        ${HOME}) local tmp_pwd="~" ;;
        '/') local tmp_pwd="/" ;;
        *) local tmp_pwd="${PWD##*/}"
    esac
    case ${USER} in
        #noby|strlst) echo -n "$_x$(echo -n \\r)$_x\e[1;35m${USER}$_x\e[1;36m@$_x\e[1;35m${HOST}$_x \e[1;36m$tmp_pwd$_x\e[1;33m$(_parse_branch)$_x\e[1;0m " ;;
        noby|strlst) echo -n "$_x$(echo -n \\r)$_x\e[1;35m${USER}$_x\e[1;36m@$_x\e[1;35m${HOST}$_x \e[1;36m$tmp_pwd$_x\e[1;0m " ;;
        #noby) echo -n "$_x\e[1;36m $tmp_pwd$_x\e[1;33m$(_parse_branch)$_x\e[1;0m " ;;
        root) echo -n "[${USER}@${HOST} ${PWD###*/}]$ " ;;
        *)    echo -n '% $tmp_pwd '
    esac
}

_get_WP() {
    local wpcount=$(($(ls ~/.config/walls/ | wc -l)))
    local rand=$((RANDOM))
    local wall=~/.config/walls/wall$(((($rand % $wpcount + $wpcount) % $wpcount) + 1)).png
    echo $wall
}

reroll() {
    feh --bg-fill $(_get_WP)
}

function stream() {
    mpv --ytdl-format=bestaudio ytdl://ytsearch:"$*"
}

function calc() {
    echo ""$*"" | bc -l
}

function calc() {
    echo "$*" | bc -l
}

transfer() {
    for i in "$@"; do
        finalResult="$(curl -sS -T $i https://transfer.sh/)"
        printf "$finalResult" | xclip -sel clip
        echo "uploaded!" "${finalResult}"
    done
}

shootscreentowebs() {
    scrot 'screenshot.jpg' -e 'mv $f /tmp/screenshot.jpg'
    transfer /tmp/screenshot.jpg
}

recency_find() {
    find . -maxdepth 2 -printf "%T@ %Tc %p\n" | sort -n
}

cd .

. ${HOME}/.profile

set -A complete_make -- build clean config configure obj install uninstall
